# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=texstudio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.8.7
pkgrel=1
pkgdesc="Integrated writing environment for creating LaTeX documents (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://texstudio.sourceforge.net/"
msys2_repository_url='https://github.com/texstudio-org/texstudio'
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hunspell"
         "${MINGW_PACKAGE_PREFIX}-poppler-qt6"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-quazip-qt6"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
optdepends=("${MINGW_PACKAGE_PREFIX}-evince: pdf reader")
source=("${msys2_repository_url}/archive/${pkgver}/${_realname}-$pkgver.tar.gz"
        '0001-install-extra-files-on-mingw.patch'
        '0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.patch'
        '0003-texstudio-4.2.3-fix-cast-to-smaller-type.patch')
sha256sums=('b8272bc6a067b7132805f8877aad9bee077e80970728cdb889edef2bb23e3b70'
            'b89f798f910e54dc6618edc09ab2834537897b5b18e0590fcb75c3530d4796fb'
            '50695b93a29f9d0078d9a46e281a2366cae36d78d4ebd54e835151041fe7d359'
            'ee2a685c006f21312700ed9ea8fee5d93f22695540375c69ede13d83a2e03407')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
    cd "${srcdir}"/${_realname}-${pkgver}

    apply_patch_with_msg \
      0001-install-extra-files-on-mingw.patch \
      0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.patch \
      0003-texstudio-4.2.3-fix-cast-to-smaller-type.patch
}

build() {
    mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

    declare -a _extra_config
    if check_option "debug" "n"; then
        _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
        _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    if [[ ${CARCH} == aarch64 ]]; then
      _extra_config+=("-DTEXSTUDIO_ENABLE_CRASH_HANDLER=OFF")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DTEXSTUDIO_ENABLE_MEDIAPLAYER=ON \
        "${_extra_config[@]}" \
        ../${_realname}-${pkgver}

    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
    cd "${srcdir}/build-${MSYSTEM}"

    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

    install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
      "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
